package com.xlh.outside;



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import com.xlh.Utils.DBManager;
import com.xlh.Utils.DataBaseHelper;
import com.xlh.broadcast.WzBroadcastReceiver;
import com.xlh.outside.R;




import android.R.bool;
import android.app.ActivityManager;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;




@SuppressLint({ "HandlerLeak", "ResourceAsColor", "ClickableViewAccessibility" })
public class BaseActivity extends FragmentActivity {


	public WzApplication getApp() {
		return app;
	}

	public void setApp(WzApplication app) {
		this.app = app;
	}

	private WzApplication app;

	protected DBManager dbManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//Log.e("runing：","BaseActivity===================================");

		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
								  WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
}
	
	
	protected void dialog() {
		AlertDialog.Builder builder = new Builder(BaseActivity.this);
		builder.setMessage("确定要退出吗?");
		builder.setTitle("系统提示");
		builder.setPositiveButton("确定",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						android.os.Process.killProcess(android.os.Process.myPid());
						System.exit(0);
					}
				});
		builder.setNegativeButton("取消",
				new android.content.DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {



		if (keyCode == KeyEvent.KEYCODE_BACK) {
			dialog();


		} else if (keyCode == KeyEvent.KEYCODE_MENU) {
		
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}


	/**
	 * 判断当前activity是否已经打开
	 * @param PackageName
	 * @param context
	 * @return
	 */
	public boolean isForeground(String PackageName,Context context){
		// Get the Activity Manager
		ActivityManager manager = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);

		// Get a list of running tasks, we are only interested in the last one,
		// the top most so we give a 1 as parameter so we only get the topmost.
		List< ActivityManager.RunningTaskInfo > task = manager.getRunningTasks(1);

		// Get the info we need for comparison.
		ComponentName componentInfo = task.get(0).baseActivity;
		Log.e("<<<<<<<<<<","---------------"+componentInfo.getClassName());
		// Check if it matches our package name.

		if(componentInfo.getClassName().equals(PackageName))
			return true;

		// If not then our app is not on the foreground.
		return false;
	}











}
